package pactera.tf.controller;

import java.util.List;
import java.util.Optional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import pactera.tf.dataobj.ChatFriendsDO;
import pactera.tf.dataobj.ChatMsgDO;
import pactera.tf.dataobj.UserinfoDO;
import pactera.tf.excpetion.R;
import pactera.tf.service.ChatFriendsService;
import pactera.tf.service.ChatMsgService;
import pactera.tf.service.LoginService;

@Controller
public class ChatController {
    @Autowired
    ChatFriendsService chatFriendsService;
    @Autowired
    LoginService loginService;
    @Resource
    private StringRedisTemplate strtemp;
    /**
     * 添加好友：查询用户
     * */
    @PostMapping("/chat/lkuser/{username}")
    @ResponseBody public R lkuser(@PathVariable("username")String username){
        String uid = loginService.lkUseridByUsername(username);
        if(uid==null){
            return R.error().message("未查询到此用户");
        }
        return R.ok().data("userinfo",chatFriendsService.lkUserinfoByUserid(uid)).message("用户信息");
    }
    /***
     * 查询用户的好友
     * */
    @PostMapping("/chat/lkfriends")
    @ResponseBody 
    public  R lkfriends(HttpSession session){
        String userid=(String)session.getAttribute("userid");
        List<ChatFriendsDO> friendsInfo = chatFriendsService.lookUserAllFriends(userid);
        return R.ok().data("friendsInfo", friendsInfo).message("好友信息");
    }
    /**
     * 添加好友
     * */
    @PostMapping("/chat/adduser/{fuserid}")
    @ResponseBody public R tofuseridchat(@PathVariable("fuserid")String fuserid,HttpSession session){
        String userid=(String)session.getAttribute("userid");
        if(userid.equals(fuserid)){
            return R.error().message("不能添加自己为好友");
        }
        ChatFriendsDO chatFriends=new ChatFriendsDO();
        chatFriends.setUserid(userid).setFuserid(fuserid);
        Integer integer = chatFriendsService.justTwoUserIsFriend(chatFriends);
        if(integer==null){
            //如果不存在好友关系插入好友关系
            chatFriendsService.insertUserFriend(chatFriends);
            chatFriendsService.insertUserFriend(new ChatFriendsDO().setFuserid(userid).setUserid(fuserid));
        }
        return R.ok().message("添加成功");
    }
    /**
     * 
    
     * <p>Title: tochat</p>  
    
     * <p>Description:跳转到聊天界面 </p>  
    
     * @return
     */
    @GetMapping("/chat/ct")
    public String tochat(){
        return "/chat/chats";
    }
    /***
     * 点赞好友
     * */
    @PostMapping("/chat/livefriend/{actuserid}")
    @ResponseBody public R likefrenf(HttpSession session, @PathVariable("actuserid")String actuserid){
        String userid=(String)session.getAttribute("userid");
        System.out.println(userid);
        String livenum = strtemp.opsForValue().get(actuserid);
        if(livenum==null) {
        	strtemp.opsForValue().set(actuserid, "1");
        }else {
        	int parseInt = Integer.parseInt(livenum)+1;
        	strtemp.opsForValue().set(actuserid, parseInt+"");	
        }
         livenum = strtemp.opsForValue().get(actuserid);
         UserinfoDO userinfoDO = new UserinfoDO();
         userinfoDO.setLivenum(livenum);
         userinfoDO.setUserid(actuserid);
        chatFriendsService.updateuselive(userinfoDO);
        return R.ok();
    }
}
